﻿using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace SalesManagementCommon.API.SalesManagementCommons.PurchaseReturnOrderRead
{
    public class PurchaseReturnOrderReadXLSHandler : IRequestHandler<PurchaseReturnOrderReadXLSCommand, FileExportResult>
    {
        private readonly IBaseRepository<PurchaseReturnOrder> _purchaseReturnOrderRepository;
        private readonly IBaseRepository<PurchaseReturnOrderSubtable> _purchaseReturnOrderSubtableRepository;

        public PurchaseReturnOrderReadXLSHandler(IBaseRepository<PurchaseReturnOrder> purchaseReturnOrderRepository, IBaseRepository<PurchaseReturnOrderSubtable> purchaseReturnOrderSubtableRepository)
        {
            _purchaseReturnOrderRepository = purchaseReturnOrderRepository;
            _purchaseReturnOrderSubtableRepository = purchaseReturnOrderSubtableRepository;
        }
        /// <summary>
        /// 采购退货单导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<FileExportResult> Handle(PurchaseReturnOrderReadXLSCommand request, CancellationToken cancellationToken)
        {
            var result = new FileExportResult();

            var list = await(await _purchaseReturnOrderRepository.GetAll()).ToListAsync();
            Console.WriteLine(list.Count);
            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet();
            IRow row1 = sheet.CreateRow(0);

            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(2).SetCellValue("单据编码");
            row1.CreateCell(3).SetCellValue("流水号");
            row1.CreateCell(4).SetCellValue("业务类型");
            row1.CreateCell(5).SetCellValue("采购单号");
            row1.CreateCell(6).SetCellValue("备注信息");
            row1.CreateCell(7).SetCellValue("单据日期");
            row1.CreateCell(8).SetCellValue("操作员");
            row1.CreateCell(9).SetCellValue("供应商");
            row1.CreateCell(10).SetCellValue("单据状态");
            row1.CreateCell(11).SetCellValue("审核员");
            row1.CreateCell(12).SetCellValue("退货仓库");
            row1.CreateCell(13).SetCellValue("业务状态");
            row1.CreateCell(14).SetCellValue("审核日期");
            row1.CreateCell(15).SetCellValue("退货原因");
            int rowIndex = 0;
            foreach (var item in list)
            {
                row1 = sheet.CreateRow(rowIndex);
                row1.CreateCell(0).SetCellValue(Convert.ToInt32(item.PurchaseReturnOrderId));
                row1.CreateCell(2).SetCellValue(item.PurchaseReturnOrderNo);
                row1.CreateCell(3).SetCellValue(item.PurchaseReturnOrderSerialNumber);
                row1.CreateCell(4).SetCellValue(Convert.ToInt32(item.BusinessType));
                row1.CreateCell(5).SetCellValue(item.PurchaseOrderNo);
                row1.CreateCell(6).SetCellValue(item.DescInfo);
                row1.CreateCell(7).SetCellValue(Convert.ToDateTime(item.DocumentDate));
                row1.CreateCell(8).SetCellValue(item.Operator);
                row1.CreateCell(9).SetCellValue(item.Supplier);
                row1.CreateCell(10).SetCellValue(Convert.ToInt32(item.DocumentsStatus));
                row1.CreateCell(11).SetCellValue(item.Audit);
                row1.CreateCell(12).SetCellValue(Convert.ToInt32(item.ReturnsWarehouse));
                row1.CreateCell(13).SetCellValue(Convert.ToInt32(item.BusinessStatus));
                row1.CreateCell(14).SetCellValue(Convert.ToDateTime(item.AuditDate));
                row1.CreateCell(15).SetCellValue(Convert.ToInt32(item.ReasonForReturn));
                rowIndex++;

            }
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                data = ms.ToArray();
            }
            string contextType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            string fileName = "采购退货单.xlsx";

            result.FileBytes = data;
            result.MimeType = contextType;
            result.FileName = fileName;
            return result;
        }
    }
}
